mui.init({
    pullRefresh: {
        container: "#refreshContainer", //下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
        down: {
            style: 'circle', //必选，下拉刷新样式，目前支持原生5+ ‘circle’ 样式
            color: '#2BD009', //可选，默认“#2BD009” 下拉刷新控件颜色
            height: '50px', //可选,默认50px.下拉刷新控件的高度,
            range: '100px', //可选 默认100px,控件可下拉拖拽的范围
            offset: '0px', //可选 默认0px,下拉刷新控件的起始位置
            auto: false, //可选,默认false.首次加载自动上拉刷新一次
            callback: function () {
                muluD.getMuluList();
                setTimeout(function () {
                    mui('#refreshContainer').pullRefresh().endPulldownToRefresh(false);
                }, 1000)
            }
        }
    }
});

var muluD;
mui.plusReady(function () {
    var self = plus.webview.currentWebview();
    var bookid = self.bookid;
    var buy_status = self.buy_status;
    var bookTitle = self.bookTitle;
    $('.mui-title').text(bookTitle);
    muluD = new Vue({
        el: '#data_list',
        data: {
            mulu_lists:'',
            notes_lists:'',
            bookmark_lists:'',
            tabIndex:1,
            loginSta:0,
        },
		mounted(){
            this.getMuluList();
            this.updateStatus();
            var tabsSwiper = new Swiper('#tabs-container', {
			    speed: 500,
			    on: {
			        slideChangeTransitionStart: function() {
			            $(".tabs .active").removeClass('active');
			            $(".tabs a").eq(this.activeIndex).addClass('active');
			        }
			    }
			})
			$(".tabs a").on('tap', function(e) {
			    e.preventDefault()
			    $(".tabs .active").removeClass('active')
			    $(this).addClass('active')
			    tabsSwiper.slideTo($(this).index())
			});
		},
		methods:{
            getMuluList:function(){
                showWatting();
                var api_host = plus.storage.getItem('HOST_URL');
                mui.ajax({
                    url:api_host+'/api/ebook/index/book_mulu',
                    dataType:'json',
                    data:{'bookid':bookid},
                    type:'get',
                    headers:{'User-Token':plus.storage.getItem('UserToken')},
                    timeout:3000,
                    success:function(msg){
                        if(msg.code == 1){
                            muluD.mulu_lists = msg.data.mulu;
                            muluD.notes_lists = msg.data.notes;
                            muluD.bookmark_lists = msg.data.bookmark;
                        }else{
                            mui.toast(msg.msg, {duration: '200',type: 'div'});
                        }
                    },
                    error:function(xhr,type,errorThrown){
                        mui.toast('请求出错，请重试', {duration: '200',type: 'div'});
                    },
                    complete:function (XMLHttpRequest,status) {
                        plus.nativeUI.closeWaiting();
                    }
                });
            },
            updateStatus:function(){
                var api_host = plus.storage.getItem('HOST_URL');
                var self = this;
                mui.ajax({
                    url: api_host + '/api/linxuan/user/checkLoginStatus',
                    dataType: 'json',
                    headers:{'User-Token':plus.storage.getItem('UserToken')},
                    timeout:3000,
                    type: 'get',
                    success: function (msg) {
                        if (msg.code == 1) {
                            self.loginSta = 1;
                        } else {
                            self.loginSta = 0;
                        }
                    },
                    error: function (xhr, type, errorThrown) {
                        mui.toast('登录状态请求失败请重试', {duration: '300',type: 'div'});
                    }
                });
            },
            open_mulu_list_detail:function (sid) {
                console.log(sid);
                if(this.loginSta == 0){
                    mui.toast('您还没有登录', {duration: '300',type: 'div'});
                }else{
                    var obj = plus.webview.all();
                    var i = 0;
                    $.each(obj,function (index,value) {
                        if(value.id == 'read.html'){
                            i ++;
                        }
                    });
                    //如果该read页面对象大于一个  就关闭顶层的
                    if(i > 1){
                        plus.webview.getWebviewById('read.html').close();
                    }
                    mui.openWindow({
                        url: 'read.html',
                        extras:{
                            'bookid':bookid,
                            'sid':sid,
                            'buyStatus':buy_status
                        },
                        createNew:true,
                        show: {
                            autoShow: true,
                            aniShow: 'pop-in',
                            duration: 400
                        },
                        waiting: {
                            autoShow: false,
                        }
                    })
                }

            },
            select_tab:function (e) {
                this.tabIndex = e.target.getAttribute('tab-index');
            }
		},
    })
})